package server;


import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import webWof.WebMessage;


public class MsgTransferServlet extends HttpServlet {
	
	private static final long serialVersionUID = -7138184628829581896L;

	public MsgTransferServlet() {
		super();
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		try {
			
			response.setContentType("application/x-java-serialized-object");
			InputStream in = request.getInputStream();
			ObjectInputStream inputFromApplet = new ObjectInputStream(in);
			java.lang.Object o =  inputFromApplet.readObject();
			WebMessage msg = null;
			if (o.getClass().equals(WebMessage.class)){
				msg = (WebMessage)o;
			}
			WebMessage retMsg = WebGamesManager.getInstance().getPendingMsg(msg);
			//return the value to client
			OutputStream outstr = response.getOutputStream();
			ObjectOutputStream oos = new  ObjectOutputStream(outstr);
			oos.writeObject(retMsg);
			oos.flush();
			oos.close();

		} catch (Exception e) { 
			e.printStackTrace();
		}		
		
		
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

	
	

}
